import store from '../store';
import coreUtil from '../utils/coreUtil.js'
import {
	getDateYYYYMMDDHHMISS
} from '../utils/dateUtil.js'
import config from '@/conf/config.js'

const URL_AGAIN_TOKEN = ['loginProperty', 'user.service.logout','community.listMyEnteredCommunitys'] //处理不需要重新获取token的接口

var BASE_URL = ''
const IMG_URL = config.assetsPath; //用于富文本获取图片
// #ifdef MP-WEIXIN || APP-PLUS
BASE_URL = config.baseUrl;
// #endif

// #ifdef H5
BASE_URL = './app/';
// #endif

var that = this
/**
 * 获取请求头信息
 **/
export function getHeaders() {
	let _cookie
	let _userId
	if (store.state.user.token) {
		_cookie = '_newly_token_=' + store.state.user.token
	}
	if (!store.state.user.userId) {
		_userId = '-1'
	} else {
		_userId = store.state.user.userId
	}
	return {
		'app-id': '992020022270580001',
		'transaction-id': coreUtil.wxuuid(),
		'req-time': getDateYYYYMMDDHHMISS(),
		'sign': '1234567',
		'user-id': '-1',
		'cookie': _cookie
	};
}

function request({
	url,
	data,
	method,
	responseType
}) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			data,
			method,
			responseType,
			header: getHeaders(),
			success: (res) => {
				console.log('res', res)
				console.log('当前时间', new Date().getTime())
				console.log('过期时间', store.state.user.tokenExpire)
				
				if (res.data.code == 0 || res.statusCode == 200 ) {
					console.log('data.code == 0')
					resolve(res.data);
				}
				if (res.statusCode == 400 || res.statusCode == 500) {
					console.log('data.code == 400')
					reject(res);
				}
				if (res.statusCode == 401) {
					console.log('data.code == 401')
					reject(res);
				}
				// 新版token过期处理
				var tokenExpire = store.state.user.tokenExpire
				var token = store.state.user.token
					if (URL_AGAIN_TOKEN.indexOf(url) == -1) {
						console.log('url',url)
						console.log('触发',tokenExpire)
						if (tokenExpire !== undefined 
						|| tokenExpire !== null) {
							if (tokenExpire !== '') {
								if (Number(tokenExpire) <= new Date().getTime()) {
									if (token !== '') {
										store.dispatch('user/cleanUserInfo') //清除vuex以及本地存储
									}
									uni.showModal({
										title: '登陆已过期',
										content: '请重新登陆',
										success: function(res) {
											if (res.confirm) {
												uni.navigateTo({
													url: '/pages/login/login/login'
												})
											} else if (res.cancel) {
												console.log('用户点击取消');
											}
										}
									});
									return;
								}
							}
						}
					}

				// 旧版token过期处理
				// if (store.state.user.token !== '' && res.data.path == '/user.html') {
				// 	uni.showModal({
				// 		title: '登陆已过期',
				// 		content: '请重新登陆',
				// 		success: function(res) {
				// 			if (res.confirm) {
				// 				store.dispatch('user/logout')  //清除vuex以及本地存储
				// 				uni.navigateTo({
				// 					url: '/pages/login/login/login'
				// 				})
				// 			} else if (res.cancel) {
				// 				console.log('用户点击取消');
				// 			}
				// 		}
				// 	});
				// }
			},
			fail: (error) => {
				// uni.showToast({
				// 	title: "~服务器异常了~",
				// 	icon: 'none',
				// 	mask: true,
				// 	duration: 3000
				// });
				reject(error);
			},
			complete: (error) => {
				// 关闭加载
				uni.hideLoading();
			}
		});
	});
}

export default request;
export {
	IMG_URL
}
export {
	BASE_URL
}
